#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <libusb-1.0/libusb.h>

#if 1 // DEBUG
	#define DBUG(format, arg...) printf("\33[32m [INFO] [%s][%d]-" format "-\33[0m\n", __func__, __LINE__, ##arg)
	#define DERR(format, arg...) printf("\33[32m [INFO] [%s][%d]-" format "-\33[0m\n", __func__, __LINE__, ##arg)
#else
	#define DBUG(format, arg...)
	#define DERR(format, arg...) 
#endif

static void print_devs(libusb_device **devs)
{
	libusb_device *dev;
	int i = 0;

	while ((dev = devs[i++]) != NULL) {
		struct libusb_device_descriptor desc;
		int r = libusb_get_device_descriptor(dev, &desc);
		if (r < 0) {
			fprintf(stderr, "failed to get device descriptor");
			return;
		}

		printf("%04x:%04x (bus %d, device %d)\n",
			desc.idVendor, desc.idProduct,
			libusb_get_bus_number(dev), libusb_get_device_address(dev));
	}
}

int main() {
    libusb_context *c;
    libusb_device_handle *h = NULL;
	libusb_device **devs;
	
	int ret = 0;

    ret = libusb_init(&c);
	DBUG("libusb_init [%d]", ret);
	if (ret) {
		DERR("libusb_init ERROR [%d]", ret);
		return ret;
	}

    libusb_set_debug(c, 1);

	ret = libusb_get_device_list(c, &devs);
	DBUG("libusb_get_device_list  [%d]", ret);
	if (ret < 0) {
		DBUG("libusb_get_device_list  [%d]", ret);
		return ret;
	}

	print_devs(devs);

	libusb_free_device_list(devs, 1);

	libusb_exit(c);
	return 0;
}
